package com.yh.controller;
import com.yh.service.IPagesService;
import com.yh.service.impl.PagesService;
import com.yh.until.GetPageSize;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class RtwoBordServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/json;charset=UTF-8");
             PrintWriter out=response.getWriter();
             JSONObject jso=new JSONObject();
            String pageIndex=request.getParameter("pageIndex");
            if (pageIndex==null){
                pageIndex="1";
            }
            IPagesService ips=new PagesService();
            int count=ips.getCount();
            int tto= GetPageSize.getPageSize(count,GetPageSize.PageSize);
            Integer index= Integer.parseInt(pageIndex);
            if(index<1){
                index=1;
            }
            if (index>tto){
                index=tto;
            }
            JSONArray jsa =ips.getPage(GetPageSize.PageSize,index);
            jso.put("dex",index);
            jso.put("tto",tto);
            jso.put("list",jsa);
            System.out.println(jso);
            out.print(jso);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
